package com.ruoyi.project.zerocarbon.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.project.zerocarbon.domain.Declaration;
import com.ruoyi.project.zerocarbon.domain.DeclareAuthor;
import com.ruoyi.project.zerocarbon.domain.DeclareJudge;
import com.ruoyi.project.zerocarbon.domain.DeclareRanking;
import com.ruoyi.project.zerocarbon.domain.dto.DeclarationDTO;
import com.ruoyi.project.zerocarbon.domain.vo.DeclarationAuditVo;
import com.ruoyi.project.zerocarbon.domain.vo.DeclarationVo;
import com.ruoyi.project.zerocarbon.mapper.*;
import com.ruoyi.project.zerocarbon.service.IDeclarationService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 参数配置 服务层实现
 * 
 * @author ruoyi
 */
@Service
public class DeclarationServiceImpl implements IDeclarationService
{
    @Autowired
    private DeclarationMapper declarationMapper;
    @Autowired
    private DeclareAuthorMapper declareAuthorMapper;
    @Autowired
    private DeclareFileMapper declareFileMapper;
    @Autowired
    private DeclareJudgeMapper declareJudgeMapper;
    @Autowired
    private DeclareRankingMapper declareRankingMapper;
    @Override
    public Declaration auditDecoration(DeclarationAuditVo declarationAuditVo) {
        Declaration declaration = new Declaration();
        declaration.setId(declarationAuditVo.getDeclarationId());
        declaration.setSaveType(declarationAuditVo.getSaveType());
        declarationMapper.updateDeclaration(declaration);
        declaration.setUpdateTime(new Date());
        return declarationMapper.selectDeclarationById(declarationAuditVo.getDeclarationId());
    }

    @Override
    public List<Declaration> getDecorationList(DeclarationVo declarationVo) {
        if (StringUtils.isEmpty(declarationVo.getRegion())){
            return new ArrayList<>();
        }
        return declarationMapper.selectDeclarationList(declarationVo);
    }

    @Override
    public DeclarationDTO getDecorationDTO(Long declarationId) {
        Declaration declarationHistory = declarationMapper.selectDeclarationById(declarationId);
        DeclarationDTO declarationDTO = new DeclarationDTO();
        if (declarationHistory != null){
            BeanUtils.copyProperties(declarationHistory,declarationDTO);
            List<DeclareAuthor> collection = declareAuthorMapper.selectByDeclarationId(declarationHistory.getId());
            declarationDTO.setDeclareAuthors(collection);
            declarationDTO.setDeclareFiles(declareFileMapper.selectByDeclarationId(declarationHistory.getId()));
            return declarationDTO;
        }
        return null;
    }

    @Override
    public int updateDeclaration(Declaration declaration) {
        declaration.setUpdateTime(DateUtils.getNowDate());
        return declarationMapper.updateDeclaration(declaration);
    }

    @Override
    public List<Declaration> getRankingDecorationList(DeclarationVo declarationVo) {
        List<Declaration> rankingDecorationList = declarationMapper.getRankingDecorationList(declarationVo);
        for (Declaration declaration:rankingDecorationList){
            List<DeclareJudge> declareJudges = declareJudgeMapper.selectDeclareJudgeByDecorationId(declaration.getId());
            declaration.setDeclareJudges(declareJudges);
            if (!CollectionUtils.isEmpty(declareJudges)){
                if (declareJudges.size() < 4){
                    declaration.setScore(0);
                }else if (declareJudges.size() == 4){
                    Integer sum = declareJudges.stream().mapToInt(DeclareJudge::getScore).sum();
                    declaration.setScore(Math.round(sum/4));
                }else if (declareJudges.size() == 5){
                    Integer sum = declareJudges.stream().mapToInt(DeclareJudge::getScore).sum();
                    declaration.setScore(Math.round(sum/5));
                }
            }else {
                declaration.setScore(0);
            }
            DeclareRanking declareRanking = declareRankingMapper.selectDeclareRankingByDeclarationId(declaration.getId());
            Integer score = 0;
            if (declareRanking != null){
                score = declareRanking.getScore();
            }
            int ranking = declarationMapper.getRankingNum(score,declaration.getManagementType()) + 1;
            declaration.setRanking(ranking);
        }
        return rankingDecorationList;
    }

//    public static void main(String[] args) {
//        List<DeclareJudge> declareJudges = new ArrayList<>();
//        DeclareJudge model1= new DeclareJudge();
//        model1.setScore(50);
//        DeclareJudge model2= new DeclareJudge();
//        model2.setScore(50);
//        declareJudges.add(model1);
//        declareJudges.add(model2);
//        Integer sum = declareJudges.stream().mapToInt(DeclareJudge::getScore).sum();
//        System.out.println(sum);
//        System.out.println(Math.round(sum/5));
//    }

    @Override
    public List<Declaration> getGroupDecorationList(DeclarationVo declarationVo) {
        List<Declaration> groupDecorationList = declarationMapper.getGroupDecorationList(declarationVo);
        return groupDecorationList;
    }

    @Override
    public List<Declaration> getJudgeDecorationList(DeclarationVo declarationVo,String expertNumber) {
        List<Declaration> groupDecorationList = declarationMapper.getGroupDecorationList(declarationVo);
        List<Declaration> list = new ArrayList<>();
        for (Declaration declaration:groupDecorationList){
            DeclareJudge declareJudge = new DeclareJudge();
            declareJudge.setDeclarationId(declaration.getId());
            declareJudge.setExpertNumber(expertNumber);
            if (declarationVo.getJudgeStatus() != null){
                declareJudge.setStatus(declarationVo.getJudgeStatus());
            }
            List<DeclareJudge> declareJudges = declareJudgeMapper.selectDeclareJudgeList(declareJudge);
            if (CollectionUtils.isNotEmpty(declareJudges)){
                declaration.setScore(declareJudges.get(0).getScore());
                declaration.setJudgeStatus(declareJudges.get(0).getStatus());
            }else {
//                if (declarationVo.getJudgeStatus() != null){
//                    continue;
//                }
                declaration.setJudgeStatus(0);
            }
            // 推荐星级
            DeclareRanking declareRanking = declareRankingMapper.selectDeclareRankingByDeclarationId(declaration.getId());
            Integer score = 0;
            if (declareRanking != null){
                score = declareRanking.getScore();
            }
            int ranking = declarationMapper.getRankingNum(score,declaration.getManagementType()) + 1;
            declaration.setRanking(ranking);
            list.add(declaration);
        }
        return list;
    }

    @Override
    public List<Declaration> getJudgeDecorationListByVoList(List<Declaration> groupDecorationList,Integer judgeStatus,String expertNumber) {
        List<Declaration> list = new ArrayList<>();
        for (Declaration declaration:groupDecorationList){
            DeclareJudge declareJudge = new DeclareJudge();
            declareJudge.setDeclarationId(declaration.getId());
            declareJudge.setExpertNumber(expertNumber);
            if (judgeStatus != null){
                declareJudge.setStatus(judgeStatus);
            }
            List<DeclareJudge> declareJudges = declareJudgeMapper.selectDeclareJudgeList(declareJudge);
            if (CollectionUtils.isNotEmpty(declareJudges)){
                declaration.setScore(declareJudges.get(0).getScore());
                declaration.setJudgeStatus(declareJudges.get(0).getStatus());
            }else {
//                if (judgeStatus != null && judgeStatus == 0){
//                    continue;
//                }
                declaration.setJudgeStatus(0);
            }
            // 推荐星级
            DeclareRanking declareRanking = declareRankingMapper.selectDeclareRankingByDeclarationId(declaration.getId());
            Integer score = 0;
            if (declareRanking != null){
                score = declareRanking.getScore();
            }
            int ranking = declarationMapper.getRankingNum(score,declaration.getManagementType()) + 1;
            declaration.setRanking(ranking);
            list.add(declaration);
        }
        return list;
    }

}
